package cloud.maque.auth.service.impl;

import cloud.maque.auth.entity.AuthUser;
import cloud.maque.auth.mapper.AuthUserMapper;
import cloud.maque.auth.service.UserService;
import cloud.maque.common.core.exceptions.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<AuthUserMapper, AuthUser> implements UserService {
    @Override
    public AuthUser getUserByUserName(String userName) {
        QueryWrapper<AuthUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AuthUser::getUserName, userName);
        return this.getOne(queryWrapper);
    }

    @Override
    public AuthUser getUserByUserId(String userId) {
        return this.getById(userId);
    }

    @Override
    public AuthUser getUserByUserPhone(String userPhone) {
        QueryWrapper<AuthUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AuthUser::getPhone, userPhone);
        return this.getOne(queryWrapper);
    }

    /**
     * 根据邮箱查询用户
     *
     * @param userEmail
     * @return
     */
    @Override
    public AuthUser getUserByUserEmail(String userEmail) {
        return null;
    }

    /**
     * 根据邮箱查询用户
     *
     * @param userEmail
     */
    @Override
    public List<AuthUser> getUserByUserEmails(String userEmail) {
        return new ArrayList<>();
    }

    @Override
    public AuthUser CreateUser(AuthUser user) {

        AuthUser oldUser = this.getUserByUserId(user.getUserId());
        if (oldUser != null) {
            throw new BusinessException("用户ID已存在");
        }
        AuthUser oldUser2 = this.getUserByUserName(user.getUserId());
        if (oldUser2 != null) {
            throw new BusinessException("用户名已存在");
        }

        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        this.baseMapper.insert(user);

        return user;
    }

    @Override
    public AuthUser GetUserByOpenId(String openId) {
        QueryWrapper<AuthUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AuthUser::getOpenId, openId);
        return this.getOne(queryWrapper);
    }

    @Override
    public void UpdateOpenId(String userId, String openId) {

        UpdateWrapper<AuthUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(AuthUser::getUserId, userId).set(AuthUser::getOpenId, openId);
        this.update(updateWrapper);
    }

    @Override
    public String getUserTotpSecret(String userId) {
        QueryWrapper<AuthUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AuthUser::getUserId, userId);
        AuthUser user = this.getOne(queryWrapper);
        if (user != null) {
            return user.getTotpSecret();
        } else {
            throw new BusinessException("用户不存在");
        }
    }

    @Override
    public Boolean updateUserTotpSecret(String userId, String totpSecret) {
        UpdateWrapper<AuthUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(AuthUser::getUserId, userId).set(AuthUser::getTotpSecret, totpSecret);
        return this.update(updateWrapper);
    }
}
